%include "asm_io.inc"

segment .data
;
;initialized data is put in the data segment here
;

prompt1 db "Give 1st number:	",0	;initialize the messages
prompt2 db "Give 2nd number:	",0
prompt3 db "Give 3rd number:	",0
prompt4 db "Give 4th number:	",0
prompt5 db "Give 5th number:	",0
outmsg1	db ",",0			;store comma
outmsg2	db 10,0				;ascii for newline
outmsg3	db "The 5 signed integers are sorted",0
outmsg4	db "The 5 unsigned integers are sorted",0

segment .bss
;
;uninitialized data is put in the bss segment
;

input1s resd 1
input2s resd 1
input3s resd 1
input4s resd 1
input5s resd 1

input1u resd 1
input2u resd 1
input3u resd 1
input4u resd 1
input5u resd 1

segment .text
;
;code here
;

	global asm_main

asm_main:
	enter 0,0		;setup routine
	pusha

	mov eax, prompt1	;prepare 1st prompt
	call print_string	;print out 1st prompt

	call read_int		;read integer
	mov [input1s],eax	;store into input1
	mov [input1u],eax	;store into input1
	
	mov eax, prompt2	;2nd prompt
	call print_string	

	call read_int		;2nd store
	mov [input2s],eax	
	mov [input2u],eax
	
	mov eax, prompt3	;3rd prompt
	call print_string

	call read_int		;3rd store
	mov [input3s],eax	
	mov [input3u],eax
	
	mov eax, prompt4	;4th prompt
	call print_string	

	call read_int		;4th store
	mov [input4s],eax	
	mov [input4u],eax
	
	mov eax, prompt5	;5th prompt
	call print_string

	call read_int		;5th store
	mov [input5s],eax	
	mov [input5u],eax
	
;signed bubblesort here
whiles:
	mov edx,0	;reset the switch counter
	
	;prepare first pair
	mov eax, [input1s]
	mov ebx, [input2s]
	
	cmp eax,ebx	;compare pair
	jng if1s	;jump if there is nothing to do
	
	mov [input1s],ebx	;do switch
	mov [input2s],eax
	
	add edx,1	;account for the switch
if1s:
	
	
	;prepare 2nd pair
	mov eax, [input2s]
	mov ebx, [input3s]
	
	cmp eax,ebx	;compare pair
	jng if2s	;jump if there is nothing to do
	
	mov [input2s],ebx	;do switch
	mov [input3s],eax
	
	add edx,1	;account for the switch
if2s:

	;prepare 3rd pair
	mov eax, [input3s]
	mov ebx, [input4s]
	
	cmp eax,ebx	;compare pair
	jng if3s	;jump if there is nothing to do
	
	mov [input3s],ebx	;do switch
	mov [input4s],eax
	
	add edx,1	;account for the switch
if3s:

	;prepare 4th pair
	mov eax, [input4s]
	mov ebx, [input5s]
	
	cmp eax,ebx	;compare pair
	jng if4s	;jump if there is nothing to do
	
	mov [input4s],ebx	;do switch
	mov [input5s],eax
	
	add edx,1	;account for the switch
if4s:
	;output our number sequence
	
	mov eax, [input1s]	
	call print_int	
	
	mov eax, outmsg1	;prepare a comma
	call print_string	;print it out
	
	mov eax, [input2s]	
	call print_int	
	
	mov eax, outmsg1	;prepare a comma
	call print_string	;print it out
	
	mov eax, [input3s]	
	call print_int	
	
	mov eax, outmsg1	;prepare a comma
	call print_string	;print it out
	
	mov eax, [input4s]	
	call print_int	
	
	mov eax, outmsg1	;prepare a comma
	call print_string	;print it out
	
	mov eax, [input5s]	
	call print_int	
	
	mov eax, outmsg2	;prepare a newline
	call print_string	;print it out
	
	cmp edx,0	;check if we did any switching
	jnz whiles	;do the jump back if we did any switching
	
	mov eax, outmsg3	;prepare the final signed message
	call print_string	;print it out
	
	mov eax, outmsg2	;prepare newline
	call print_string	;print it out

;unsigned bubblesort here	
whileu:
	mov edx,0	;reset the switch counter
	
	;prepare first pair
	mov eax, [input1u]
	mov ebx, [input2u]
	
	cmp eax,ebx	;compare pair
	jb if1u	;jump if there is nothing to do
	
	mov [input1u],ebx	;do switch
	mov [input2u],eax
	
	add edx,1	;account for the switch
if1u:
	
	;prepare 2nd pair
	mov eax, [input2u]
	mov ebx, [input3u]
	
	cmp eax,ebx	;compare pair
	jb if2u	;jump if there is nothing to do
	
	mov [input2u],ebx	;do switch
	mov [input3u],eax
	
	add edx,1	;account for the switch
if2u:

	;prepare 3rd pair
	mov eax, [input3u]
	mov ebx, [input4u]
	
	cmp eax,ebx	;compare pair
	jb if3u	;jump if there is nothing to do
	
	mov [input3u],ebx	;do switch
	mov [input4u],eax
	
	add edx,1	;account for the switch
if3u:
	
	;prepare 4th pair
	mov eax, [input4u]
	mov ebx, [input5u]
	
	cmp eax,ebx	;compare pair
	jb if4u	;jump if there is nothing to do
	
	mov [input4u],ebx	;do switch
	mov [input5u],eax
	
	add edx,1	;account for the switch
if4u:
	;output our number sequence
	mov eax, [input1u]	
	call print_int	
	
	mov eax, outmsg1	;prepare a comma
	call print_string	;print it out
	
	mov eax, [input2u]	
	call print_int	
	
	mov eax, outmsg1	;prepare a comma
	call print_string	;print it out
	
	mov eax, [input3u]	
	call print_int	
	
	mov eax, outmsg1	;prepare a comma
	call print_string	;print it out
	
	mov eax, [input4u]	
	call print_int	
	
	mov eax, outmsg1	;prepare a comma
	call print_string	;print it out
	
	mov eax, [input5u]	
	call print_int	
	
	mov eax, outmsg2	;prepare a newline
	call print_string	;print it out

	cmp edx,0	;check if we did any switching
	jnz whileu	;do the jump back if we did any switching
	
	mov eax, outmsg4	;prepare the final signed message
	call print_string	;print it out
	
	mov eax, outmsg2	;prepare newline
	call print_string	;print it out
	
	;do not remove code after this
	popa
	mov eax,0	;return back to C
	leave
	ret
